package models
{
	import com.asfusion.mate.events.Dispatcher;
	
	import events.settings.ApplicationSettingEvent;
	
	import flash.events.Event;
	
	import models.misc.SettingsManager;
	import models.vos.ApplicationSetting;
	public class ApplicationSettingsManager
	{
		private var dp:Dispatcher = new Dispatcher();
		public function ApplicationSettingsManager()
		{
		}
		
		[Bindable(Event="applicationSettingChanged")]
		public var appSetting:ApplicationSetting;
		
		public function getSettings(evt:ApplicationSettingEvent):void{
			//create settings if currently null
			if(SettingsManager.getSetting('time') == null){
	 			SettingsManager.saveSetting('locales', [
	 				{name:"Tiếng Việt", value:"vi_VN"},{name:"English", value:"en_US"}
	 				]);
	 			SettingsManager.saveSetting('time', new Date());
	 			SettingsManager.saveSetting('dateformat', 'DD/MM/YY');
	 		}
	 		
	 		appSetting = new ApplicationSetting();
	 		appSetting.localeDate = SettingsManager.getSetting('time') as Date;
	 		appSetting.localeLangs = SettingsManager.getSetting('locales') as Array;
	 		appSetting.dateFormat = SettingsManager.getSetting('dateformat') as String;
	 		
			dp.dispatchEvent( new Event("applicationSettingChanged") );
			switchLocale();
		}
		public function saveApplicationSettings(settings:ApplicationSetting):void{
			
			SettingsManager.saveSetting('time', settings.localeDate);
			SettingsManager.saveSetting('locales', settings.localeLangs);
			SettingsManager.saveSetting('dateformat', settings.dateFormat);
			this.appSetting = settings;
			dp.dispatchEvent( new Event("applicationSettingChanged") );
			switchLocale();
			dp.dispatchEvent( new ApplicationSettingEvent( ApplicationSettingEvent.SAVE_SUCCESS ));
		}
		
		private function switchLocale():void{
			Locale.switchLocale( appSetting.localeLangs );
		}
	}
}